const numberReg = /^-?[1-9][0-9]?.?[0-9]*$/
const intReg = /^-?[1-9][0-9]*$/
const phoneReg = /^1[0-9]{10,10}$/
const emailReg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
const pwdReg = /^.{6,16}$/
const inviteCodeReg = /^[a-zA-Z0-9]{6,16}$/

export default {
	isMobile(data) {
		return numberReg.test(val)
	},
	isInt: function(val) {
		return intReg.test(val)
	},
	isPhone: function(val) {
		return phoneReg.test(val)
	},
	isEmail: function(val) {
		return emailReg.test(val)
	},
	isPwd: function(val) {
		return pwdReg.test(val)
	}, 
	isInviteCode: function(val) {
		return inviteCodeReg.test(val)
	},
	validate(data, rules, all = false) {
		let res = {}
		return new Promise((resolve, reject) => {
			for (let key in data) {
				const value = data[key]
				if (rules.hasOwnProperty(key)) {
					const fieldRule = rules[key]
					for (let i = 0; i < fieldRule.length; i++) {
						if (fieldRule[i].required) {
							if (value.length == 0) {
								res[key] = fieldRule[i].errMsg
								if(all === false) reject(fieldRule[i].errMsg)
							}
						}
					}
				}
			}
			if(Object.keys(res).length == 0) {
				resolve(data)
			} else {
				reject(res)
			}
			
		})
	},
	
	fordataInfo(data) {
		let res = {}
		return new Promise((resolve, reject) => {
			for (let key in data) {
				// const value = data[key]
					console.log(key)
			}
		})
	},
	validates(data, rules) {
		return this.validate(data, rules, true)
	}
	
	
}